﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Common;

public class dazhao : MonsterAction
{
    public override void Update()
    {
        base.Update();
        process();
    }

    public override void Start()
    {
        base.Start();//need do first

        Animation.wrapMode = WrapMode.Loop;
        Handle.CrossAnimation(Animation.name, 0);
        Handle.Gravity = false;
        Handle.State = MonsterState.None;
        Handle.DoExcursion = false;
        dHeight = Handle.Position.y;
    }

    private bool attacked = false;
    private float dHeight = 0;

    private float duration;
    private bool up = true;
    private const float height = 7;
    private byte jumpAllCount = 10;
    private byte jumpCount = 0;
    private float jumpSpeed = 30;
    private Vector3 speed = Vector3.zero;
    private float walkSpeed = 12;
    private void process()
    {
        if (Handle.Dead) return;
        if (up)
        {
            attacked = false;
            Handle.Controller.Move(Vector3.up * jumpSpeed * InputManager.DeltaTime);
            if (Handle.Position.y >= dHeight + height)
            {
                up = false;
            }
        }
        else
        {
            if (AppInterface.AvatarModule.IOwner.Position.z > Handle.Position.z) Handle.Direction = MovementType.Right;
            else Handle.Direction = MovementType.Left;


            if (Mathf.Abs(AppInterface.AvatarModule.IOwner.Position.z - Handle.Position.z) > 1.0f)
            {
                if (Handle.Direction == MovementType.Left) speed.z = -walkSpeed;
                else speed.z = walkSpeed;
            }
            else speed.z = 0;
            speed.y = -jumpSpeed;
            if ((Handle.Controller.Move(speed * InputManager.DeltaTime) & CollisionFlags.CollidedBelow) != 0)
            {
                up = true;
                jumpCount++;
                if (jumpCount >= jumpAllCount)
                {
                    up = false;
                    Handle.State = MonsterState.Stand;
                    Handle.DoExcursion = true;
                    Handle.Gravity = true;
                    End = true;
                }
            }
            if (Define.Mode == GameMode.Teamwork)
            {
                if (!attacked && AppInterface.AvatarModule.IOwner.Collider.bounds.Intersects(Handle.Collider.bounds))
                {
                    AppInterface.AvatarModule.IOwner.OnBeat(this, UnityEngine.Random.Range(-4, -1), Handle as IUnit, true);
                    attacked = true;
                }
            }
        }
    }
}

